//
//  String+Extensions.swift
//  XLWB
//
//  Created by MacBook on 16/7/10.
//  Copyright © 2016年 MacBook. All rights reserved.
//
//正则表达式
import Foundation

extension String {

    //从当前字符串中,提取链接和文本
    //Swift提供了"元组",同事返回多个值
    //在oc当中,可以返回字典/字典一对象/指针的指针
    func dd_href() -> (link: String, text: String)? {
    
        //匹配方案
        let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"
        
        //创建正则表达式,并且匹配第一项
        guard let regx = try? RegularExpression(pattern: pattern, options: []),
        result = regx.firstMatch(in: self, options: [], range: NSRange(location: 0, length: characters.count))
           
             else {
                
            return nil
        }
    
        //获取结果
        let link = (self as NSString).substring(with: result.range(at: 1))
        let text = (self as NSString).substring(with: result.range(at: 2))
        
        return (link, text)
    
    }
}
